package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.management.Myself;

import javax.management.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.management.ManagementFactory;

public class JMXServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("enter--doGet");
		super.doGet(req, resp);
		System.out.println("exit--doGet");
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("enter--init");
		super.init();
//		MBeanServer server = MBeanServerFactory.createMBeanServer("org.geektimes.web.mvc");
		MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
		ObjectName objectName ;
		try {
			// 为 UserMXBean 定义 ObjectName
			objectName = new ObjectName("MyselfMBean:type=Myself");
			Myself myself = new Myself();
			// 创建 UserMBean 实例
			mBeanServer.registerMBean(myself, objectName);
//			while (true) {
//				Thread.sleep(2000);
//				System.out.println(myself);
//			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}